<?php
/**
 * Created by PhpStorm.
 * User: apple
 * Date: 2020/10/12
 * Time: 4:06 PM
 */
namespace app\admin\logic;

use app\model\Goods;
use app\model\GoodsExchange;
use app\model\Video;
use think\facade\Request;

class VideoLogic
{

    public function getList()
    {
        $param = Request::get();

        $where = [];
        if(isset($param['videoName']) && !empty($param['videoName'])){
            $where[] = ['v_name','like','%'.trim($param['videoName']).'%'];
        }

        if(isset($param['createtime']) && !empty($param['createtime']) && $param['createtime'] > 0){
            $startTime = strtotime(date('Y-m-d',$param['createtime']));
            $endTime = strtotime(date('Y-m-d 23:59:59',$param['createtime']));
            $where[] = ['v_create_time','>=',$startTime];
            $where[] = ['v_create_time','<=',$endTime];
        }

        $where[] = ['v_status','=',1];    //默认是 没删除的
        $where[] = ['v_orgid','=',config('user')['orgid']];
        $videoModel = new Video();
        $list = $videoModel->getVideoListByCondition($where);
        return $list;
    }

    public function addVideo()
    {
        $param = Request::post();

        $video = [
            'v_orgid' => config('user.orgid')
        ];
        if(!isset($param['videoName']) || empty($param['videoName'])) fail('请输入视频标题');
        $video['v_name'] = trim($param['videoName']);

        if(!isset($param['videoUrl']) || empty($param['videoUrl'])) fail('请上传视频');
        $video['v_url'] = fileFullPath(trim($param['videoUrl']));

        $videoModel = new Video();
        $rt = $videoModel->addOneVideo($video);
        if(!$rt)    fail('视频添加失败');

        return $rt;
    }

    public function updateVideo()
    {
        $param = Request::post();

        if(!isset($param['videoId']) || empty($param['videoId'])) fail('请选择视频');
        $videoId = intval($param['videoId']);
        $videoModel = new Video();
        $info = $videoModel->getVideoInfoById($videoId);
        if(!$info)  fail('视频不存在');

        $video = [];
        if(!isset($param['videoName']) || empty($param['videoName'])) fail('请输入视频标题');
        $video['v_name'] = trim($param['videoName']);

        if(!isset($param['videoUrl']) || empty($param['videoUrl'])) fail('请上传视频');
        $video['v_url'] = fileFullPath(trim($param['videoUrl']));

        $rt = $videoModel->updateVideoInfoByWhere(['v_id'=>$videoId],$video);
        if(!$rt)    fail('视频修改失败');

        return $rt;
    }

    public function del($videoId)
    {
        $videoModel = new Video();
        $videoInfo = $videoModel->getVideoInfoById($videoId);

        if(!$videoInfo) fail('视频不存在');

        if($videoInfo['v_status'] == 0)   fail('视频已删除');

        $goodsExchange = [
            'v_status' => 0,
        ];
        $rt = $videoModel->updateVideoInfoByWhere(['v_id'=>$videoId],$goodsExchange);
        if(!$rt)    fail('兑换失败');

        return $rt;
    }

}